package vfrpt

import java.text.SimpleDateFormat

class WipDetailController {

    def dynamicQueryService

    def wipDetail() {
        SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd')
        def startTime = params.startTime ? sdf.parse(params.startTime) : sdf.parse(sdf.format(new Date().clearTime()))
        def endTime = params.endTime ? sdf.parse(params.endTime) : sdf.parse(sdf.format(new Date()))
        params.startTime = sdf.format(startTime)
        params.endTime = sdf.format(endTime)
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 100
        params.startno = (pageSize * (pageCurrent - 1)) + 1
        params.endno = pageSize * pageCurrent
        def tableHeadList = []
        def wipDetail = []
        def pageSizeList = [10, 20, 50, 100]
        def totalCount = 0
        if (params.EXPORT_CONTENT_NAME == 'Excel') {
            params.SYS_QUERY_NAME = 'wipDetail'
            wipDetail = dynamicQueryService.queryForList(params)
            if (wipDetail.size() > 0) {
                tableHeadList = wipDetail[0].keySet()
            }
            params.SYS_QUERY_NAME = 'wipDetailCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        }
        [
                startno      : params.startno ?: '',
                endno        : params.endno ?: '',
                pageSize     : pageSize,
                totalCount   : totalCount,
                pageSizeList : pageSizeList,
                tableHeadList: tableHeadList,
                wipDetail    : wipDetail,
                startTime    : params.startTime ?: '',
                endTime      : params.endTime ?: '',
                partName     : params.partName ?: '',
                tuhao        : params.tuhao ?: '',
                startDocId   : params.startDocId ?: '',
                endDocId     : params.endDocId ?: '',
                stepName     : params.stepName ?: '',
                stepDesc     : params.stepDesc ?: ''
        ]
    }

    def exportExcel() {
        def sysQueryName
        def tableName1
        def tableName2
        def name
        sysQueryName = params.SYS_QUERY_NAME

        params.startno = 1
        params.endno = 99999999999999999


        params.SYS_QUERY_NAME = sysQueryName

        def dataList = dynamicQueryService.queryForList(params)

        def tableHeadList = []
        if (dataList.size() > 0) {
            tableHeadList = dataList[0].keySet()
        }
        tableName1 = params.TABLE_NAME1
        tableName2 = params.TABLE_NAME2

        if (tableName1 != null && tableName1 != "" && tableName2 != null && tableName2 != "") {
            name = tableName1 + "-" + tableName2
        } else {
            name = sysQueryName
        }
        String filename = new String("${name}.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        render(template: "export${params.EXPORT_CONTENT_NAME}", model: [dataList: dataList, tableHeadList: tableHeadList, startno: params.startno ?: '', endno: params.endno ?: ''])
    }
}
